package cn.stylefeng.guns.modular.web.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.web.entity.Gold;
import cn.stylefeng.guns.modular.web.service.IGoldService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：365天打卡金币奖励设置表 controller
 * 作者：chenhuiyan
 * 时间：2020-04-18
 */
@Controller
@RequestMapping("/web/gold")
public class GoldController extends BaseController {

    private String PREFIX = "/modular/web/gold/";

    @Autowired
    private IGoldService goldService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "gold.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = goldService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转生成365天金币页面
     */
    @RequestMapping(value = "/goldNumDayIndex")
    public String goldNumDayIndex() {
        return PREFIX + "gold_add.html";
    }

    /**
     * 生成365天金币
     * @param goldNum
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/goldNumDay")
    public ResponseData goldNumDay(@RequestParam(name = "goldNum") Integer goldNum){
        if(goldNum < 0){
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        goldService.goldNumDay(goldNum);
        return SUCCESS_TIP;
    }

    /**
     * 跳转修改金币信息页面
     */
    @RequestMapping(value = "/editIndex")
    public String editIndex(String goldId, Model model) {
        if (ToolUtil.isEmpty(goldId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("goldId", goldId);
        return PREFIX + "gold_edit.html";
    }

    /**
     * 修改金币信息
     * @param gold
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit")
    public ResponseData edit(Gold gold){
        if (ToolUtil.isEmpty(gold.getId())) {
            throw new ServiceException(BizExceptionEnum.PARAM_ERROR);
        }
        gold.preUpdate();
        goldService.updateById(gold);
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param goldId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{goldId}")
    public Object getInfoById(@PathVariable("goldId") String goldId){
        Gold gold = goldService.getById(goldId);
        return gold;
    }

}
